package com.ld.security.server.admin.modules.ge.service.impl;

import com.ld.security.common.mybatis.base.po.PagePo;
import com.ld.security.common.mybatis.base.vo.DataPerNode;
import com.ld.security.common.mybatis.base.vo.PageVo;
import com.ld.security.common.mybatis.util.JdbcUtil;
import com.ld.security.server.admin.modules.ge.entity.GeViewTelEntity;
import com.ld.security.server.admin.modules.ge.service.IGeViewService;
import com.ld.security.server.admin.modules.ge.service.IGeViewTelService;
import com.ld.security.common.core.base.SuperPo;
import com.ld.security.common.core.base.column.ListJson;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.core.util.TemplateUtil;
import com.ld.security.common.mybatis.base.service.ISuperServiceImpl;
import com.ld.security.server.admin.modules.ge.util.GeScopeUtil;
import com.ld.security.server.admin.modules.ge.vo.GeViewTelVo;
import com.ld.security.server.admin.modules.ge.vo.ViewTelSaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
class GeViewTelServiceImpl extends ISuperServiceImpl<GeViewTelEntity> implements
        IGeViewTelService {



    @Autowired
    IGeViewService iGeViewService;

    private void updateFile(GeViewTelEntity o) {
        String code = TemplateUtil.renderTemplate(o.getTemplateFile(), null);
        o.setTemplateCode(code);
        this.save(o);
    }

    @Override
    public int save(GeViewTelEntity entity) {

        if (entity.getModelArgs() == null || entity.getModelArgs().isEmpty()) {
            ListJson listJson = new ListJson();
            entity.setModelArgs(listJson);
        }
        int total = super.save(entity);

        return total;
    }




    @Override
    public List<GeViewTelEntity> cloneData(SuperPo po) {
        List<GeViewTelEntity> list =  this.all(po);
        for (GeViewTelEntity viewTel : list) {
            viewTel.setId(null);
        }
        this.saveBatch(list);
        return list;
    }

    @Override
    public ViewTelSaveVo getViewTelSaveVo(String id) {
        ViewTelSaveVo vo = new ViewTelSaveVo();
        vo.setId(id);
        this.proxyQuery(vo);
        return vo;
    }

    @Override
    public void saveViewTel(ViewTelSaveVo po) {
        if(StringUtil.isNotNull(po.getViewTel().getId())){
            ViewTelSaveVo oldVo =  getViewTelSaveVo(po.getViewTel().getId());
            this.proxyRemove(oldVo);
        }

        this.proxySave(po);
    }

    @Override
    public PageVo<GeViewTelEntity> page(PagePo po) {

        return super.page(po);
    }

    @Override
    public PageVo<GeViewTelEntity> dataPerPage(PagePo<GeViewTelEntity> po) {

    GeScopeUtil.queryBefore(po.getQuery());

        PageVo<GeViewTelEntity> pageVo =  JdbcUtil.page(po);

        GeScopeUtil.queryAfter(pageVo.getList());


        return pageVo;
    }
}
